package deap;

import java.util.Comparator;

public class Test {
    public class IntCmp implements Comparator<Integer>{

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2-o1;
        }
    }
    public int[] smallestK(int[] arr, int k) {
        if(k == 0||arr.length == 0){
            return new int[0];
        }
        //前k建立一个大根堆 每次与堆顶比较 小于堆顶入堆 返回堆中的数据
        java.util.PriorityQueue<Integer> priorityQueue = new java.util.PriorityQueue<>(new IntCmp());
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }
        for (int i = k; i < arr.length; i++) {
            if(arr[i]<priorityQueue.peek()){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }
        int[] result = new int[k];
        for (int i = 0; i < k; i++) {
            result[i] = priorityQueue.poll();
        }
        return result;

    }
    public static void main(String[] args) {

    }
    public static void main1(String[] args) {
        int [] a = {27,15,19,18,28,34,65,49,25,37};
        PriorityQueue priorityQueue = new PriorityQueue();
        priorityQueue.createHeap(a);
        priorityQueue.print();
        System.out.println("=============");
        priorityQueue.push(50);
        priorityQueue.print();
        System.out.println();
        priorityQueue.pollHeap();
        priorityQueue.print();
    }
}
